package com.css.fxfzypg.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.SysUserRoleTypeEnum;
import com.css.fxfzypg.modules.system.service.SysServices;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.ResponseCreator;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年04月21日  9:54
 */

@Slf4j
@RestController
@RequestMapping("/sys")
public class SysController {

    @Resource
    SysServices sysServices;

    @Resource
    SUserService sUserService;

    /**
     * 获取用户是国家级还是省级
     */
    @GetMapping("/getUserRoleType")
    public RestResponse<SysUserRoleTypeEnum> getUserRoleType(@RequestParam String userId) {
        return ResponseCreator.createSuccessResponse(sysServices.getUserRoleType(userId));
    }

    /**
     * 判断用户是否是国家级
     */
    @GetMapping("/userIsCountry")
    public RestResponse<Map<String, Object>> userIsCountry(@RequestParam String userId) {
        try {
            Map<String, Object> map = new HashMap<>();
            boolean b = sysServices.userIsCountry(userId);
            map.put("isCountry", b);
            if (!b) {
                String userJson = sUserService.getSUser(userId);
                SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
                map.put("provinceId", sUser.getProvinceId());
            }
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 判断用户是否是国家级
     */
    @GetMapping("/getRole")
    public RestResponse<List<JSONObject>> getRole(@RequestParam String userId) {
        return ResponseCreator.createSuccessResponse(sysServices.getRole(userId));
    }
}
